package com.zfzn.flink;

import com.google.gson.Gson;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeoutException;

/**
 * @Auther lolineet
 * @Email tj.zhanghongwei@163.com
 * @Date 2021/8/19 14:28
 * @Description
 */
public class Producer {
    public final static String QUEUE_NAME="zfzn";  //队列的名字
    public static void main(String[] args) throws IOException, IOException, TimeoutException, InterruptedException {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();

        //设置RabbitMQ相关信息
        factory.setHost("127.0.0.1");
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setPort(5672);

        Connection connection = factory.newConnection();
        //如果是string就不需要序列化了
        Transaction transaction = new Transaction();
        transaction.setAccountId(11111111L);
        Transaction transaction2= new Transaction();
        transaction2.setAccountId(22222222L);
        //set 赛数据，就不写了。 //序列化
        Random random = new Random(1);
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
        int i = 0;
        while(true) {
            if(i%7!=1) {
                transaction.setAmount(random.nextDouble());
            }else {
                transaction.setAmount((double) random.nextInt(50000));
            }
            transaction2.setAmount(random.nextDouble());
            Gson gson = new Gson();
            String str= gson.toJson(transaction);
            String str2=gson.toJson(transaction2);
            channel.basicPublish("", QUEUE_NAME, null, str.toString().getBytes("utf-8"));
            channel.basicPublish("", QUEUE_NAME, null, str2.toString().getBytes("utf-8"));
            Thread.sleep(5000);
            i++;
        }
        //关闭通道和连接
        //channel.close();
        //connection.close();
    }
}
